import { CollectionUtils } from "@chulei-ui/utils";
import type { UniverseSystem } from "./UniverseSystem";
import type ExcelJs from "@scvzerng/exceljs";

export class UniverseStation {
  station_id!: number;
  name!: string;
  system?: UniverseSystem;

  static fromSheet(sheet: ExcelJs.Worksheet, systems: UniverseSystem[]) {
    const stations: UniverseStation[] = [];
    const systemMap = CollectionUtils.array2Map<UniverseSystem>(systems, (system: UniverseSystem) => system.system_id);
    sheet.eachRow((row, index) => {
      if (index > 1) {
        const id = row.getCell(1).text!;
        const name = row.getCell(2).text!;
        const systemId = Number(row.getCell(3).text!);
        const station = new UniverseStation();
        const system = systemMap[systemId];
        station.station_id = Number(id);
        station.name = name;
        station.system = system;
        if (system) {
          system.stations.push(station);
        }
        stations.push(station);
      }
    });
    return stations;
  }
}
